/**
 * 详情对话框
 */
var BasicAddressInfoDlg = {
    data: {
        putgoodsName: "",
        putgoodsId: "",
        subsidiary: "",
        address: "",
		longitude:"",
		latitude:"",
		kilometer:"",
        contacts: "",
        tel: "",
        deliverId: "",
        dept_id: ""
    }
};
/**
 * 添加或者修改页面
 */
var OrderTransportInfoDlg = {
    data: {
    	setUnitId: "",
        setUnit: "",
        setLocation: "",
        setPeople: "",
        setTel: "",
        setMentiontime: "",
        setWarehousepeople: "",
        setWarehousetel: "",
        setAddress: "",
        getUnitId: "",
        getUnit: "",
        getLocation: "",
        getPeople: "",
        getTel: "",
        getMentiontime: "",
        getWarehousepeople: "",
        getWarehousetel: "",
        getAddress: "",
    }
};
layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/basicAddress/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('basicAddressForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicAddress/editItem", function (data) {
            Feng.success("Update successful!");
	    	parent.submit = 1;
        	var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
        }, function (data) {
            Feng.error("Update failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    //选择位置
    $("#selectlocation").click(function(){
        layer.open({
            type: 2,
            title: 'Select Address',
            area: ['800px', '500px'],
            content: Feng.ctxPath + '/basicAddress/toChoiceAddress?address=' + $("#address").val()+"&longitude=" + $("#longitude").val()+"&latitude=" + $("#latitude").val(), 
            end: function () {
				if(BasicAddressInfoDlg.data.address != null && BasicAddressInfoDlg.data.address != ''){
	                $("#address").val(BasicAddressInfoDlg.data.address);//地址
	                $("#longitude").val(BasicAddressInfoDlg.data.longitude);//经度
	                $("#latitude").val(BasicAddressInfoDlg.data.latitude);//维度
				}
            }
        });
    });
    
    //计算距离
    $("#selectdistance").click(function(){
            layer.open({
            type: 2,
            title: 'Calculate Optimal Route',
            area: ['800px', '500px'],
            content: Feng.ctxPath + '/outOrder/toSelectdistance?set_longitude=' + $("#set_longitude").val() + "&set_latitude=" + $("#set_latitude").val()
								  + "&get_longitude=" + $("#longitude").val()+ "&get_latitude=" + $("#latitude").val()
								  + "&kilometer=" + $("#kilometer").val(), 
            end: function () {
				if(BasicAddressInfoDlg.data.kilometer != null && BasicAddressInfoDlg.data.kilometer != ''){
                    var kilometerWithUnit = BasicAddressInfoDlg.data.kilometer;
                    if (/\u7C73/.test(kilometerWithUnit)) { // \u7C73 is Unicode for "meter"   //Regular expression .test returns true if "meter" is in the expression
                        var match = kilometerWithUnit.match(/\d+/);
                        if (match && match.length > 0) {
                            var meterValue = parseInt(match[0]); // Extract the number and convert to integer
                            var kilometerValue = meterValue / 1000;
                            $("#kilometer").val(kilometerValue.toFixed(2));
                        }
                    }else {
                        $("#kilometer").val(kilometerWithUnit);
                    }

				}
            }
        });
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});